package com.lagou;

import com.lagou.bean.ConsumerComponent;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;


public class AnnotationConsumerMain {
    public static void main(String[] args) throws IOException, InterruptedException {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
        context.start();
        ConsumerComponent service = context.getBean(ConsumerComponent.class);
        /*while (true) {
            for (int i=0;i<3000;i++){
                Thread.sleep(5);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        int ms = (int) (Math.random()*100);
                        String  msg =  service.methodA("world",ms);
                        String  msgB =  service.methodB("world",ms);
                        String  msgC =  service.methodC("world",ms);
                        System.out.println(msg);
                        System.out.println(msgB);
                        System.out.println(msgC);
                    }
                }).start();
            }
        }*/
        ExecutorService m= Executors.newFixedThreadPool(1);
        ExecutorService m2= Executors.newFixedThreadPool(1);
        ExecutorService m3= Executors.newFixedThreadPool(1);

        for(int i=1;i<=700;i++){
            m.submit(new Runnable(){
                @Override
                public void run() {
                    int ms = (int) (Math.random()*100);
                    String  msg =  service.methodA("A",ms);
                }
            });
            m2.submit(new Runnable(){
                @Override
                public void run() {
                    int ms = (int) (Math.random()*100);
                    String  msg =  service.methodB("B",ms);
                }
            });
            m3.submit(new Runnable(){
                @Override
                public void run() {
                    int ms = (int) (Math.random()*100);
                    String  msg =  service.methodC("C",ms);
                }
            });
        }
        if(!m.awaitTermination(60, TimeUnit.SECONDS)){
            // 超时的时候向线程池中所有的线程发出中断(interrupted)。
            m.shutdownNow();
            m2.shutdownNow();
            m3.shutdownNow();
        }
    }

    @Configuration
    @PropertySource("classpath:/dubbo-consumer.properties")
    //@EnableDubbo(scanBasePackages = "com.lagou.bean")
    @ComponentScan("com.lagou.bean")
    @EnableDubbo
    static class ConsumerConfiguration {

    }
}
